<?php
namespace Admin\Controller;
use Think\Controller;
class ArticleController extends CommonController
{
    function index() {
        $model = M('Article');
        $searchname = I('keyword');
        if ($searchname) {
            $map['id|title|author'] = array(intval($searchname), array('like', '%' . $searchname . '%'), array('like', '%' . $searchname . '%'), '_multi' => true);
        }
        $category = I('category');
        if ($category) {
            $category = getallchildcateids($category);
            $map['category_id'] = array('in', $category);
        }
        $list = $this->lists($model, $map);
        foreach ($list as $key => $value) {
            $category_name = get_category_name($value['category_id']);
            $list[$key]['category_name'] = $category_name ? $category_name : null;
        }
        $this->assign('list', $list);
        $this->assign('searchname', $searchname);
        $this->display();
    }
    public function add() {
        if (IS_POST) {
            $data['title'] = I('post.title');
            $data['keyword'] = I('post.keyword');
            $data['category_id'] = I('post.category_id');
            $data['description'] = I('post.description');
            $data['pic'] = I('post.pic');
            $data['content'] = I('post.content', '', NULL);
            $data['addtime'] = time();
            $model = M('Article');
            $rs = $model->create($data);
            if (!$rs) {
                $this->error($model->getError());
            } else {
                $rs = $model->add($data);
                if ($rs) {
                    $this->success('添加成功!');
                } else {
                    $this->error('添加失败!');
                }
            }
        } else {
            $model = M('Category');
            $data = $model->select();
            $this->assign('category', $data);
            $this->display();
        }
    }
    public function edit($id = null) {
        if (IS_POST) {
            $data = I('post.');
            $data['addtime'] = time();
            $model = M('Article');
            $rs = $model->create($data);
            if (!$rs) {
                $this->error($model->getError());
            } else {
                $rs = $model->save();
                if ($rs) {
                    $this->success('添加成功!');
                } else {
                    $this->error('添加失败!');
                }
            }
        } else {
            
            $model = M('Article');
            $map['id'] = I('id');
            $data = $model->where($map)->find();
            $category = M('Category');
            $list = $category->field('id,pid,name')->select();
            $category_name = $category->where(array('id' => $data['category_id']))->getField('name');
            $this->assign('category_name', $category_name);
            $this->assign('category', $list);
            $data['content'] = htmlspecialchars_decode($data['content']);
            $this->assign('data', $data);
            $this->display();
        }
    }
    public function upload() {
        $upload = new \Think\Upload();
        
        // 实例化上传类
        $upload->maxSize = 3145728;
        
        // 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');
        
        // 设置附件上传类型
        $upload->rootPath = './Public/Uploads/';
        
        // 设置附件上传根目录
        $upload->savePath = 'image/';
        
        // 设置附件上传（子）目录
        $upload->subName = array('date', 'Ymd');
        
        // 上传文件
        $info = $upload->upload();
        if (!$info) {
            
            // 上传错误提示错误信息
            $this->error($upload->getError());
        } else {
            
            // 上传成功
            foreach ($info as $file) {
                $url = "/Public/Uploads/" . $file['savepath'] . $file['savename'];
            }
            $this->success('上传成功!', $url);
        }
    }
    
    //删除文章
    public function del($ids = null) {
        empty($ids) && $this->error('参数错误！');
        $ids = is_array($ids) ? implode(',', $ids) : $ids;
        $map['id'] = array('in', $ids);
        $res = M('Article')->where($map)->delete();
        if ($res !== false) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败！');
        }
    }
}
